package com.esri.android.map.popup;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.esri.android.map.MapView;
import com.esri.android.map.popup.Popup;
import com.esri.android.map.popup.PopupContainerView;
import java.util.ArrayList;
import java.util.Iterator;

/* loaded from: classes.dex */
public class PopupContainer {
    PopupContainerView b;
    ViewPager.OnPageChangeListener c;
    private Context d;
    int a = 0;
    private ArrayList<PopupContainerListener> f = new ArrayList<>();
    private a e = new a();

    /* loaded from: classes.dex */
    public interface PopupContainerListener {
        void onAllPopupsRemoved();

        void onCurrentPopupChanged(int i, int i2);

        void onPopupAdded(Popup popup);

        void onPopupRemoved(Popup popup);
    }

    public PopupContainer(MapView mapView) {
        this.d = mapView.getContext();
        this.b = new PopupContainerView(this.d, new PopupContainerView.b() { // from class: com.esri.android.map.popup.PopupContainer.1
            @Override // com.esri.android.map.popup.PopupContainerView.b, androidx.viewpager.widget.ViewPager.OnPageChangeListener
            public void onPageSelected(int i) {
                PopupContainer.this.a = i;
                super.onPageSelected(i);
            }
        });
        this.b.a.setAdapter(this.e);
    }

    public PopupContainerView getPopupContainerView() {
        return this.b;
    }

    public void addPopup(final Popup popup) {
        if (popup == null) {
            return;
        }
        Context context = this.d;
        if (context instanceof Activity) {
            ((Activity) context).runOnUiThread(new Runnable() { // from class: com.esri.android.map.popup.PopupContainer.2
                @Override // java.lang.Runnable
                public void run() {
                    PopupContainer.this.e.a(popup);
                    if (!PopupContainer.this.f.isEmpty()) {
                        Iterator it = PopupContainer.this.f.iterator();
                        while (it.hasNext()) {
                            ((PopupContainerListener) it.next()).onPopupAdded(popup);
                        }
                    }
                    PopupContainer.this.e.notifyDataSetChanged();
                    popup.a(new Popup.InternalPopupListener() { // from class: com.esri.android.map.popup.PopupContainer.2.1
                        @Override // com.esri.android.map.popup.Popup.InternalPopupListener
                        public void onChangedEditMode(boolean z) {
                            PopupContainer.this.b.a.a(!z);
                        }
                    });
                }
            });
        }
    }

    public void removePopup(final Popup popup) {
        if (popup == null) {
            return;
        }
        Context context = this.d;
        if (context instanceof Activity) {
            ((Activity) context).runOnUiThread(new Runnable() { // from class: com.esri.android.map.popup.PopupContainer.3
                @Override // java.lang.Runnable
                public void run() {
                    PopupContainer.this.e.b(popup);
                    if (!PopupContainer.this.f.isEmpty()) {
                        Iterator it = PopupContainer.this.f.iterator();
                        while (it.hasNext()) {
                            ((PopupContainerListener) it.next()).onPopupRemoved(popup);
                        }
                    }
                    PopupContainer.this.e.notifyDataSetChanged();
                }
            });
        }
    }

    public void clearPopups() {
        Context context = this.d;
        if (context instanceof Activity) {
            ((Activity) context).runOnUiThread(new Runnable() { // from class: com.esri.android.map.popup.PopupContainer.4
                @Override // java.lang.Runnable
                public void run() {
                    PopupContainer.this.e.a();
                    if (!PopupContainer.this.f.isEmpty()) {
                        Iterator it = PopupContainer.this.f.iterator();
                        while (it.hasNext()) {
                            ((PopupContainerListener) it.next()).onAllPopupsRemoved();
                        }
                    }
                    PopupContainer.this.e.notifyDataSetChanged();
                }
            });
        }
    }

    public Popup getCurrentPopup() {
        return this.e.a(this.a);
    }

    public void setCurrentPopup(final int i, final boolean z) {
        Context context = this.d;
        if (context instanceof Activity) {
            ((Activity) context).runOnUiThread(new Runnable() { // from class: com.esri.android.map.popup.PopupContainer.5
                @Override // java.lang.Runnable
                public void run() {
                    PopupContainer.this.b.setCurrentPopup(i, z);
                }
            });
        }
    }

    public int getCurrentPopupIndex() {
        return this.a;
    }

    public int getPopupCount() {
        return this.e.getCount();
    }

    public void addPopupContainerListeners(PopupContainerListener popupContainerListener) {
        if (popupContainerListener != null) {
            this.f.add(popupContainerListener);
        }
    }

    public void removePopupContainerListeners(PopupContainerListener popupContainerListener) {
        this.f.remove(popupContainerListener);
    }

    public void removeAllPopContainerListeners() {
        this.f.clear();
    }

    /* loaded from: classes.dex */
    private class a extends PagerAdapter {
        private ArrayList<Popup> b = new ArrayList<>();

        public a() {
        }

        public void a(Popup popup) {
            this.b.add(popup);
        }

        public void b(Popup popup) {
            this.b.remove(popup);
        }

        public Popup a(int i) {
            if (i >= this.b.size()) {
                return null;
            }
            return this.b.get(i);
        }

        public void a() {
            this.b.clear();
        }

        @Override // androidx.viewpager.widget.PagerAdapter
        public void destroyItem(ViewGroup viewGroup, int i, Object obj) {
            viewGroup.removeView((View) obj);
            if (i >= this.b.size()) {
                return;
            }
            this.b.get(i).b();
        }

        @Override // androidx.viewpager.widget.PagerAdapter
        public Object instantiateItem(ViewGroup viewGroup, int i) {
            if (i >= this.b.size()) {
                return null;
            }
            com.esri.android.map.popup.a a = this.b.get(i).a();
            viewGroup.addView(a, -1, -1);
            return a;
        }

        @Override // androidx.viewpager.widget.PagerAdapter
        public int getCount() {
            return this.b.size();
        }

        @Override // androidx.viewpager.widget.PagerAdapter
        public boolean isViewFromObject(View view, Object obj) {
            return ((com.esri.android.map.popup.a) obj) == view;
        }
    }
}
